import { oops } from "../../../../../extensions/oops-plugin-framework/assets/core/Oops";
import { ecs } from "../../../../../extensions/oops-plugin-framework/assets/libs/ecs/ECS";
import { smc } from "../../common/ecs/SingletonModuleComp";
import { TableEquipmentLvConf } from "../../common/table/TableEquipmentLvConf";
import { Equip } from "../Equip";
/**
 * 检查装备是否可以升级
 */
@ecs.register('EquipUpgradeCheck')
export class EquipUpgradeCheckComp extends ecs.Comp {
    /** 当前等级升级消耗配置 */
    levelSpendCurrent: TableEquipmentLvConf = new TableEquipmentLvConf();
    /** 可以升几级 */
    lv: number = 0;
    /** 升级消耗 */
    levelSpend: number = 0;

    //检查装备
    check(equip: Equip) {
        //初始化等级和花费
        this.lv = 0;
        this.levelSpend = 0;
        //获取装备模型和等级模型
        var rm = equip.EquipModel;
        var rlm = equip.EquipModelLevel;
        //初始化颜色配置
        rlm.colorConfCurrent.init(rm.color);

        //初始化花费字典和总花费
        var spendDict: any = { 0: 0 };
        var spendAll = 0;
        //初始化等级
        var i = 0;
        //获取角色精铁
        var jingtie = smc.account.AccountModel.jingtie;
        //获取最大等级
        var maxLevel = Math.min(rlm.colorConfCurrent.max, smc.account.AccountModel.leader.LeaderModel.level);

        //循环计算升级花费
        while (true) {
            //初始化当前等级花费
            this.levelSpendCurrent.init(rm.level + i);
            //累加总花费
            spendAll += this.levelSpendCurrent.lv_spend;
            //将当前等级花费存入字典
            spendDict[i + 1] = spendAll;
            //如果总花费超过精铁或者等级达到3999，则退出循环
            if (spendAll > jingtie || rm.level + i == 3999) {
                break;
            }
            //等级加1
            i++;
        }

        //升级
        //如果等级加50不超过最大等级且花费不超过精铁，则升级50级
        if (rm.level + 50 <= maxLevel && spendDict[50] <= jingtie) {
            this.lv = 50;
            this.levelSpend = spendDict[50];
        }
        //如果等级加10不超过最大等级且花费不超过精铁，则升级10级
        else if (rm.level + 10 <= maxLevel && spendDict[10] <= jingtie) {
            this.lv = 10;
            this.levelSpend = spendDict[10];
        }
        //如果等级加5不超过最大等级且花费不超过精铁，则升级5级
        else if (rm.level + 5 <= maxLevel && spendDict[5] <= jingtie) {
            this.lv = 5;
            this.levelSpend = spendDict[5];
        }
        else if (rm.level + 1 <= maxLevel && spendDict[1] <= jingtie) {
            this.lv = 1;
            this.levelSpend = spendDict[1];
        }
        else {
            if (spendDict[1] <= jingtie) {
                this.lv = 1;
            }
            else {
                this.lv = 0;
            }
            this.levelSpend = spendDict[1];
        }

        //根据动态计算结果赋值
        rm.isJinjie = rm.level == rlm.colorConfCurrent.max ? 1 : 0;
        rm.spend = this.levelSpend;
        rm.upgradeLevel = this.lv;
        rm.role.RoleModelEquip.canAllUpgrade = this.lv > 0 ? 1 : 0;
        if (rm.role.pos) {
            if (rm.isJinjie > 0 || rm.upgradeLevel >= 10) {
                //@ts-ignore
                oops.rp.updateRedPointCnt(RedPointConfig[`equip_${rm.role.pos}_${rm.type}`], 1)
            } else {
                //@ts-ignore
                oops.rp.updateRedPointCnt(RedPointConfig[`equip_${rm.role.pos}_${rm.type}`], 0)
            }
        }
    }

    reset() {
    }
}